#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char s1[maxn],s2[maxn];
LL a[maxn],b[maxn];
LL fa[maxn],siz[maxn];
LL find(LL x){
    if(fa[x]!=x) return fa[x]=find(fa[x]);
    return fa[x];
}
void merge(LL x,LL y){
    if(find(x)!=find(y)){
        siz[find(y)]+=siz[find(x)];
        fa[find(x)]=find(y);
    }
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    LL n;cin>>n;
    for(LL i=1;i<=30;i++) fa[i]=i,siz[i]=1;
    cin>>(s1+1);cin>>(s2+1);
    for(LL i=1;i<=n;i++){
        a[i]=s1[i]-'a'+1;b[i]=s2[i]-'a'+1;
    }
    bool flag=1;
    for(LL i=1;i<=n;i++){
        if(a[i]>b[i]){
            cout<<"-1"<<"\n";
            flag=0;break;
        }
    }
    if(flag==0) continue;
    for(LL i=1;i<=n;i++){
        if(a[i]<b[i]){
            if(find(a[i])!=find(b[i])){
                merge(a[i],b[i]);
            }
        }
    }
    LL ans=0;
    for(LL i=1;i<=30;i++){
        if(fa[i]==i){
            ans+=(siz[i]-1);
        }
    }
    cout<<ans<<"\n";
  }
return 0;
}
